﻿#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <functional>
#include <climits>

#define quickio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"

using namespace std;
typedef long long ll;
//https://leetcode.cn/problems/maximum-number-of-operations-to-move-ones-to-the-end/description/
// 
// 
// 
//// 分析样例啊！！！！！！！！！！！！！！！！要所有1移到最右边

//// 最大操作次数：从左往右挪
//// 用map存下标和个数
///*该代码本地答案正确，力扣上答案不对*/
//// 力扣上答案不对，改为数组试试
//#include <unordered_map>
//const int N = 1e5 + 1;
//unordered_map<int, int>mp;
//class Solution {
//public:
//    int maxOperations(string s)
//    {
//        long long num = atoi(s.c_str());
//        if (!num)
//            return 0;
//
//        int size = s.size();
//        num = 0;
//
//        int num1 = 0;
//        for (int i = size - 1; i >= 0; i--)
//        {
//            if (s[i] == '1')
//                num1++;
//            else
//                break;
//        }
//
//
//        int flag = 0;
//        int temp = 0, idx = 0;
//        for (int i = 0; i < size - num1; i++)
//        {
//            if (s[i] == '1' && flag == 0)
//            {
//                flag = 1;
//                temp = 1;
//                idx = i;
//            }
//            else if (s[i] == '1')
//            {
//                temp++;
//            }
//            else if (temp != 0)
//            {
//                mp.insert({ idx, temp });
//                flag = 0;
//                temp = 0;
//            }
//
//        }
//
//        int ans = 0, pre = 0;
//        for (auto& ch : mp)
//        {
//            ch.second += pre;
//            ans += ch.second;
//            pre = ch.second;
//        }
//        cout << ans << endl;
//        return ans;
//    }
//};
//
//
//// // 数组可以，unordered_map不行
//// const int N = 1e5 + 1;
//// int a[N];
//
//// class Solution2 {
//// public:
////     int maxOperations(string s)
////     {
////         memset(a, 0, sizeof(a));
//
////         // long long num = stoi(s);        
////         long long num = atoi(s.c_str());
//
////         if (!num)
////             return 0;
//
////         int size = s.size();
////         num = 0;
//
////         int num1 = 0;
////         for (int i = size - 1; i >= 0; i--)
////         {
////             if (s[i] == '1')
////                 num1++;
////             else
////                 break;
////         }
//
//
////         int flag = 0;
////         int temp = 0, idx = 0;
////         for (int i = 0; i < size - num1; i++)
////         {
////             if (s[i] == '1' && flag == 0)
////             {
////                 flag = 1;
////                 temp = 1;
////                 idx = i;
////             }
////             else if (s[i] == '1')
////             {
////                 temp++;
////             }
////             else if (temp != 0)
////             {
////                 a[idx] = temp;
////                 flag = 0;
////                 temp = 0;
////             }
//
////         }
//
////         int ans = 0, pre = 0;
////         for (int i = 0; i < size; i++)
////         {
////             if (a[i] != 0)
////             {
////                 a[i] += pre;
////                 ans += a[i];
////                 pre = a[i];
////             }
////         }
////         cout << ans << endl;
////         return ans;
////     }
//// };
//
//int main()
//{
//    Solution a;
//    a.maxOperations("1001101");
//    return 0;
//}


//题解区灵茶山艾府 题解
//堵车法：https://leetcode.cn/problems/maximum-number-of-operations-to-move-ones-to-the-end/solutions/2851730/du-che-pythonjavacgo-by-endlesscheng-tllv/
// 从左往右遍历，如果s[i] == 1,则cnt++;车的数量++
//                           0, s[i - 1] == 1,则答案ans += cnt；对每辆堵住的车都操作一次

class Solution {
public:
    int maxOperations(string s)
    {
        int size = s.size();// 减少了for每循环一次就算一次大小，提高效率
        int cnt = 0;// 记录堵住的车的数量
        int ans = 0;// 记录答案
        for (int i = 0; i < size; i++)
        {
            if (s[i] == '1')
                cnt++;
            else if (i /**/ && (s[i - 1] == '1'))
                ans += cnt;
        }

        return ans;
    }
};